Bag Generic Class

Wintellect PowerCollections

Collapse imageExpand ImageCollapseAll imageExpandAll imageDropDown imageDropDownHover imageCopy imageCopyHover image
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Bag<T> is a collection that contains items of type T. Unlike a Set, duplicate items (items that compare equal to each other) are allowed in an Bag.

Namespace: Wintellect.PowerCollections
Assembly:  PowerCollections (in PowerCollections.dll)

Syntax

C#
[SerializableAttribute]
public class Bag<T> : CollectionBase<T>, ICloneable
Visual Basic (Declaration)
<SerializableAttribute> _
Public Class Bag(Of T) _
	Inherits CollectionBase(Of T) _
	Implements ICloneable
Visual C++
[SerializableAttribute]
generic<typename T>
public ref class Bag : public CollectionBase<T>, 
	ICloneable

Type Parameters

T

Remarks

The items are compared in one of two ways. If T implements IComparable<T> then the Equals method of that interface will be used to compare items, otherwise the Equals method from Object will be used. Alternatively, an instance of IComparer<T> can be passed to the constructor to use to compare items.

Bag is implemented as a hash table. Inserting, deleting, and looking up an an element all are done in approximately constant time, regardless of the number of items in the bag.

When multiple equal items are stored in the bag, they are stored as a representative item and a count. If equal items can be distinguished, this may be noticable. For example, if a case-insensitive comparer is used with a Bag<string>, and both "hello", and "HELLO" are added to the bag, then the bag will appear to contain two copies of "hello" (the representative item).

OrderedBag<(Of <T>)> is similar, but uses comparison instead of hashing, maintain the items in sorted order, and stores distinct copies of items that compare equal.

Inheritance Hierarchy

System..::Object
  Wintellect.PowerCollections..::CollectionBase<(Of <T>)>
    Wintellect.PowerCollections..::Bag<(Of <T>)>

See Also